Imports System.IO
Imports System.Reflection
Imports System.Collections.Generic
Imports System.Linq
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Audio
Imports Microsoft.Xna.Framework.Content
Imports Microsoft.Xna.Framework.GamerServices
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework.Input
Imports Microsoft.Xna.Framework.Media
Imports Microsoft.Xna.Framework.Input.Touch

Class PressPlate
	Inherits Tile
	Private Shared tileSequence As New List(Of Sequence)()
	Public switchButton As Integer = 0
	Public elapsedTimeOpen As Single = 0
	Public timeOpen As Single = 0.3F

	Public ReadOnly Property State() As Enumeration.StateTile
		Get
			Return tileState.Value().state
		End Get
	End Property

	Public Sub New(room As RoomNew, Content As ContentManager, tileType As Enumeration.TileType, state As Enumeration.StateTile, switchButton As Integer, NextTileType__1 As Enumeration.TileType)
		MyBase.room = room
		nextTileType = NextTileType__1
		Me.switchButton = switchButton
		Dim ax As New System.Xml.Serialization.XmlSerializer(tileSequence.[GetType]())

		Dim txtReader As Stream = Microsoft.Xna.Framework.TitleContainer.OpenStream(PrinceOfPersiaGame.CONFIG_PATH_CONTENT & PrinceOfPersiaGame.CONFIG_PATH_SEQUENCES & tileType.ToString().ToUpper() & "_sequence.xml")
		'TextReader txtReader = File.OpenText(PrinceOfPersiaGame.CONFIG_PATH_CONTENT + PrinceOfPersiaGame.CONFIG_PATH_SEQUENCES + tileType.ToString().ToUpper() + "_sequence.xml");

		tileSequence = DirectCast(ax.Deserialize(txtReader), List(Of Sequence))

		For Each s As Sequence In tileSequence
			s.Initialize(Content)
		Next

		'Search in the sequence the right type
		Dim result As Sequence = tileSequence.Find(Function(s As Sequence) s.name = state.ToString().ToUpper())

		If result IsNot Nothing Then
			'AMF to be adjust....
			result.frames(0).SetTexture(Content.Load(Of Texture2D)(PrinceOfPersiaGame.CONFIG_TILES & result.frames(0).value))

			collision = result.collision
			Texture = result.frames(0).texture
		End If
		Type = tileType


		'change statetile element
		tileState.Value().state = state
		tileAnimation.PlayAnimation(tileSequence, tileState.Value())
	End Sub

	Public Sub Normal()
		If tileState.Value().state = Enumeration.StateTile.normal Then
			Return
		End If

		tileState.Add(Enumeration.StateTile.normal)
		tileAnimation.PlayAnimation(tileSequence, tileState.Value())
	End Sub

	Public Sub DePress()
		If tileState.Value().state = Enumeration.StateTile.pressplate Then
			Return
		End If

		tileState.Add(Enumeration.StateTile.pressplate)
		tileAnimation.PlayAnimation(tileSequence, tileState.Value())
	End Sub

	Public Sub Press()
		elapsedTimeOpen = 0
		If tileState.Value().state = Enumeration.StateTile.dpressplate Then
			Dim listNew As List(Of Tile) = room.maze.GetTiles(Enumeration.TileType.gate)
			For Each t As Tile In listNew
				If DirectCast(t, Gate).switchButton = Me.switchButton Then
					DirectCast(t, Gate).Open()
				End If
			Next


			Return
		End If



		tileState.Value().state = Enumeration.StateTile.dpressplate
		tileAnimation.PlayAnimation(tileSequence, tileState.Value())

		'i will open all door with the correct switchButton
		Dim list As List(Of Tile) = room.maze.GetTiles(Enumeration.TileType.gate)
		For Each t As Tile In list
			If DirectCast(t, Gate).switchButton = Me.switchButton Then
				DirectCast(t, Gate).Open()
			End If
		Next


	End Sub

End Class
